﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;
namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OJobGoal : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OJobGoal";
            }
        }

        protected int Year
        {
            get
            {
                return GetRequestValue<int>("year", DateTime.Now.Year);
            }
        }
        protected int Month
        {
            get
            {
                return GetRequestValue<int>("Month", DateTime.Now.Month);
            }
        }
        protected string colMonth
        {
            get
            {
                return Year.ToString() + "-" + Month.ToString();
            }
        }
        protected DateTime stated
        {
            get
            {
                return Convert.ToDateTime(colMonth);
            }
        }

        protected DateTime endd
        {
            get
            {
                return stated.AddMonths(1).AddDays(-1);
            }
        }

        protected object getdate(string mactype, string itemtype, string colname, string color)
        {
            string statedate = colMonth + "-" + colname.Split(new char[] { '-' })[0];
            string enddate = colMonth + "-" + colname.Split(new char[] { '-' })[1] + " 23:59:59";

            return OJobController.GetReport(mactype, itemtype, statedate, enddate, color);
        }

        protected object getdate2(string mactype, string itemtype, string color)
        {
            string statedate = colMonth + "-01 00:00:00";
            string enddate = Convert.ToDateTime(statedate).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd 23:59:59");
            return OJobController.GetReport(mactype, itemtype, statedate, enddate, color);
        }

        protected string GetYearList()
        {
            string liTemplete = "<option value='{0}' {1}>{0}年</option>";
            string rHtml = "";
            int nowYear = DateTime.Now.Year;
            for (int i = (nowYear - 3); i <= nowYear; i++)
            {
                rHtml = rHtml + string.Format(liTemplete, i, i == Year ? "selected" : "");
            }
            return rHtml;
        }

        protected string GetGoalTable(string mactype, string color)
        {
            //创建目标表
            DataTable dt = new DataTable();
            dt.Columns.Add(string.Format("{0}年{1}月", Year, Month), typeof(string));
            DateTime everyday = stated;

            string colstatelable = "01";
            while (everyday <= endd)
            {
                if (everyday.DayOfWeek == DayOfWeek.Sunday)
                {
                    colstatelable = colstatelable + "-" + everyday.ToString("dd");
                    dt.Columns.Add(colstatelable, typeof(int));
                    colstatelable = everyday.AddDays(1).ToString("dd");
                }
                everyday = everyday.AddDays(1);
            }
            if (endd.DayOfWeek != DayOfWeek.Sunday)
            {
                colstatelable = colstatelable + "-" + endd.ToString("dd");
                dt.Columns.Add(colstatelable, typeof(int));
            }
            //
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<table class='list'>");
            //第一步，创建表头HTML
            sb.AppendLine("<tr>");
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                // dr[i] = getdate(mactype, dr[0].ToString(), dt.Columns[i].ColumnName, color);
                sb.AppendFormat("<th>{0}</th>", dt.Columns[i].ColumnName);
            }
            sb.AppendLine("</tr>");
            //创建行
            string[] items = new string[] { "下线目标", "入库目标" };
            foreach (string item in items)
            {
                sb.AppendLine("<tr>");
                sb.AppendFormat("<th>{0}</th>", item);
                for (int i = 1; i < dt.Columns.Count; i++)
                {
                    string goalkey = GetInputName(mactype + color, item, dt.Columns[i].ColumnName);
                    object goalvalue = OJobController.OJobGoal_Get(goalkey);
                   // goalvalue = goalvalue is nu ? "0" : goalvalue;

                    sb.AppendFormat("<td><input type='text' value='{1}' class='ojobgoalinput' name='{0}'></input></td>", goalkey, goalvalue);
                }
                sb.AppendLine("</tr>");
            }

            sb.AppendLine("</table>");
            return sb.ToString();

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                edit();
                
            }
        }
        /// <summary>
        /// 获取输入框值
        /// </summary>
        /// <param name="mactypecolor"></param>
        /// <param name="item"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        private string GetInputName(string mactypecolor,string item,string column)
        {
            return string.Format("{0}{1}{2}{3}{4}", mactypecolor,item,column,Year,Month);
        }

        protected void edit()
        {
           // Response.Write(Month);
            string[] items = new string[] { "下线目标", "入库目标" };
            string[] mactypecolors = new string[] { "55NSL", "65NS", "80NS", "65NSL绿", "65NSL", "40NS", "100NS", "100NST","68NS","18VX" };
            //
            DataTable dt = new DataTable();
            DateTime everyday = stated;

            string colstatelable = "01";
            while (everyday <= endd)
            {
                if (everyday.DayOfWeek == DayOfWeek.Sunday)
                {
                    colstatelable = colstatelable + "-" + everyday.ToString("dd");
                    dt.Columns.Add(colstatelable, typeof(int));
                    colstatelable = everyday.AddDays(1).ToString("dd");
                }
                everyday = everyday.AddDays(1);
            }
            if (endd.DayOfWeek != DayOfWeek.Sunday)
            {
                colstatelable = colstatelable + "-" + endd.ToString("dd");
                dt.Columns.Add(colstatelable, typeof(int));
            }
            //
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                foreach (string item in items)
                {
                    foreach (string mactypecolor in mactypecolors)
                    {                       
                        string goalkey = GetInputName(mactypecolor, item, dt.Columns[i].ColumnName);
                        string goalvalue = GetRequestValue<string>(goalkey, "0");
                        OJobController.OJobGoal_Edit(goalkey, goalvalue);
                    }
                }
            }

            SuccessJson("成功保存");
        }

      
    }
}